/**
 * @author   Ivan Andonov
 * @email:   ivan.andonov[at]gmail[dot]com
 * 
 * @require  sjs 
 * @use      sjs.Utils, sjs.Debug
 * @optional 
 **/

(function() {
	
	sjs.create('sjs.Box', {
		
		initialize : function(el, xMin, xMax, yMin, yMax) {
			
			this.xMin = xMin || 0;
			this.xMax = xMax || 0;
			this.yMin = yMin || 0;
			this.yMax = yMax || 0;
			
			if (el) {
				var x = sjs.Utils.getElementX(el);
				var y = sjs.Utils.getElementY(el);
				var w = sjs.Utils.getElementWidth(el);
				var h = sjs.Utils.getElementHeight(el);
				this.xMin = x;
				this.xMax = x+w;
				this.yMin = y;
				this.yMax = y+h;
			}
			
			this.toString = function() {
				return '{ xMin:'+this.xMin+', xMax:'+this.xMax+', yMin:'+this.yMin+', yMax:'+this.yMax+' }';
			};
			
			this.combine = function(box) {
				if (box) {
					return new Box(
						null,
						Math.min(this.xMin, box.xMin),
						Math.max(this.xMax, box.xMax),
						Math.min(this.yMin, box.yMin),
						Math.max(this.yMax, box.yMax)
					);
				}
				$log('ERROR: undefined box: '+box);
				return null;
			};
			
			this.intersect = function(box) {
				if (box) {
					return new Box(
						null,
						Math.max(this.xMin, box.xMin),
						Math.min(this.xMax, box.xMax),
						Math.max(this.yMin, box.yMin),
						Math.min(this.yMax, box.yMax)
					);
				}
				$log('ERROR: undefined box: '+box);
				return null;
			};
			
			this.hitTest = function(point) {
				return point.x >= this.xMin && point.x <= this.xMax && point.y >= this.yMin && point.y <= this.yMax;
			};
		}
		
	});
	
})();